<?php
namespace app\admin\controller;

use app\common\controller\SysAction;
use app\common\model\MemberWallet;
use app\common\model\MemberWalletLogs;
use app\common\model\MemberLevel;
use think\Db;

class Member extends SysAction
{
    protected $modelClass = '\app\common\model\Member';
    
    public function initialize()
    {
        parent::initialize();
    }
    
    /**
     * @Title: index
     * @Description: todo(会员列表)
     * @return mixed
     * @author 若谷
     * @date 2019年6月11日
     * @throws
     */
    public function index()
    {
        $where = [];
        if (input('get.search')){
            $where[] = ['phone', 'like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
        //dump($dataList);exit;
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    /**
     * @Title: detail
     * @Description: todo(会员)
     * @return mixed
     * @author 若谷
     * @date 2019年6月11日
     * @throws
     */
    public function detail($id){
        $where[] = ['id','=',$id];
        if (input('get.search')){
            $where[] = ['text', 'like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $data =$this->cModel->where($where)->find();
        $data['info'] = $data->MemberInfo;
        $data['info']['province1'] = db('area')->where(['area_id'=>$data['info']['province']])->value('area_name');
        $data['info']['city1'] = db('area')->where(['area_id'=>$data['info']['city']])->value('area_name');
        $data['info']['district1'] = db('area')->where(['area_id'=>$data['info']['district']])->value('area_name');
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * @Title: addgrade
     * @Description: todo(增加/减少会员积分)
     * @return mixed
     * @author 若谷
     * @date 2019年6月11日
     * @throws
     */
    public function addgrade($id){
        if (request()->isPost()) {
            $data = input("post.");
            $data['mid'] = $id;
            $result = $this->validate($data, 'app\common\validate\Member.add');
            if(true !== $result){
                return ajax_return($result);
            }
            $info = model('Member')->where("id",$id)->find();
            $money = $data['money']+$info['money'];
            $moneyLog = [
                'uid'    => $data['mid'] ,
                'money'  => $data['money'],
                'name' => "后台充值",
                'pay_type' => 1,
                'createtime' => time(),
            ];
            Db::startTrans();
            try{
                $result = db('member')->where(['id'=>$id])->update(['money'=>$money]);
                $result1 =db('member_money_logs')->insert($moneyLog);
                // 提交事务
                if ($result && $result1){
                    Db::commit();
                    return ajax_return(lang('action_success'), url('index'));
                }else{
                    return ajax_return($this->cModel->getError());
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajax_return($e->getMessage());
            }
        }else{
            $where[] = ['id','=',$id];
            $member = $this->cModel->where($where)->field("id")->find();
            $this->assign("member",$member->memberInfo->nickName);
            return $this->fetch();
        }
    }

    
}